package com.ciwong.epaper.modules.epaper.ui.newlistenspeak.shortcoversation.adapter;

import android.content.Context;
import android.support.v7.widget.RecyclerView;
import android.util.DisplayMetrics;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.WindowManager;
import android.widget.TextView;

import com.ciwong.epaper.R;
import com.ciwong.epaper.modules.dbbean.LswAnswer;
import com.ciwong.epaper.modules.epaper.bean.ModuleSet;
import com.ciwong.epaper.modules.epaper.bean.Questions;
import com.ciwong.epaper.modules.epaper.ui.newlistenspeak.shortcoversation.view.BaseOptionsLayout;
import com.ciwong.epaper.modules.epaper.ui.newlistenspeak.shortcoversation.view.ImageTextOptionLoayout;

import java.util.List;

/**
 * Created by Administrator on 2018/4/26.
 */

public class ShortcoversationAdapter extends RecyclerView.Adapter {

    private Context context;
    private int TYPE = -1;
    private final int SREM_TOTAL = 0;//题干格式
    private final int SREM_SMALL_QUESTION = 1;//每个小题格式
    private LayoutInflater inflater;
    private List<Questions> questions;
    private OnClickForAnswerListener onClickForAnswerListener;
    private ModuleSet moduleSet;
    private int lastViewHeigh = 0;


    public ShortcoversationAdapter(Context context) {
        this.context = context;
        inflater = LayoutInflater.from(context);
        WindowManager wm = (WindowManager) context.getSystemService(Context.WINDOW_SERVICE);
        DisplayMetrics dm = new DisplayMetrics();
        wm.getDefaultDisplay().getMetrics(dm);
        // 屏幕高度（像素）
        int height = dm.heightPixels;
        lastViewHeigh = (int) (0.6 * height);
    }

    public interface OnClickForAnswerListener {
        void onclickForAnswer(LswAnswer lswAnswer, int position);
    }

    public void setOnClickForAnswerListener(OnClickForAnswerListener onClickForAnswerListener) {
        this.onClickForAnswerListener = onClickForAnswerListener;

    }

    public void setData(ModuleSet moduleSet, List<Questions> questions) {//多个题的集合
        this.moduleSet = moduleSet;
        this.questions = questions;
        notifyDataSetChanged();
    }

    public void setHightLight(int positionHightLight) {
        if (moduleSet != null) {
            moduleSet.setHighlight(false);
            if (positionHightLight == 0) {//对大标题设置高亮
                moduleSet.setHighlight(true);
            }
        }

        if (questions != null) {
            for (int i = 0; i < questions.size(); i++) {
                questions.get(i).getTrunk().setHighlight(false);
            }
            if (positionHightLight > 0) {
                questions.get(positionHightLight - 1).getTrunk().setHighlight(true);
            }
        }
        notifyDataSetChanged();
    }

    @Override
    public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        RecyclerView.ViewHolder holder = null;
        if (viewType == SREM_TOTAL) {
            View inflate = inflater.inflate(R.layout.item_short_conversaton_stem, parent, false);
            holder = new ShortConversationStemViewHolder(inflate);
        } else if (viewType == SREM_SMALL_QUESTION) {
            View inflate = inflater.inflate(R.layout.item_short_dialog, parent, false);
            holder = new ShortConversationViewHolder(inflate);
        }
        return holder;
    }

    @Override
    public void onBindViewHolder(RecyclerView.ViewHolder holder, final int position) {
        if (holder instanceof ShortConversationStemViewHolder) {
            ShortConversationStemViewHolder mHolder = (ShortConversationStemViewHolder) holder;
            if (moduleSet == null) return;
            mHolder.tvStemSotal.setSelected(moduleSet.isHighlight());
            mHolder.tvStemSotal.setText(moduleSet.getContent());
        } else if (holder instanceof ShortConversationViewHolder) {

            if (questions == null) return;
            ShortConversationViewHolder mHolder = (ShortConversationViewHolder) holder;
            mHolder.itolShortConversation.loadOptionLayout(questions.get(position - 1), true);
            mHolder.itolShortConversation.setIClickForAnswer(new BaseOptionsLayout.IClickForAnswer() {
                @Override
                public void clickForAnswer(LswAnswer lswAnswer, int position) {
                    if (onClickForAnswerListener != null)
                        onClickForAnswerListener.onclickForAnswer(lswAnswer, position);
                }
            });
        }
    }

    @Override
    public int getItemCount() {
        int count = 0;
        if (questions == null) {
            if (moduleSet == null) count = 0;
            else count = 1;
        } else {
            count = questions.size() + 1;
        }
        return count;
    }

    @Override
    public int getItemViewType(int position) {
        if (position == 0) {
            TYPE = SREM_TOTAL;
        } else {
            TYPE = SREM_SMALL_QUESTION;
        }
        return TYPE;
    }

    class ShortConversationViewHolder extends RecyclerView.ViewHolder {

        private View itemView;
        private ImageTextOptionLoayout itolShortConversation;

        public ShortConversationViewHolder(View itemView) {
            super(itemView);
            this.itemView = itemView;
            itolShortConversation = itemView.findViewById(R.id.itol_short_dialog);
        }
    }

    class ShortConversationStemViewHolder extends RecyclerView.ViewHolder {

        private View itemView;
        private TextView tvStemSotal;

        public ShortConversationStemViewHolder(View itemView) {
            super(itemView);
            this.itemView = itemView;
            tvStemSotal = itemView.findViewById(R.id.tv_stem_total);
        }
    }

}
